//
//  WiFiSettingsVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/21.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class WiFiSettingsVC: RootVC {
    private let datas = [("SSID Settings","SSIDSettingsVC"),
                         ("WPS","WPSSettingsVC"),
                         ("Wi-Fi Sleep Mode",""),
                         ("Wireless MAC Filter","MACFilterVC")]
    
    private let sleepModeName = ["Never",
                                 "10 minutes",
                                 "30 minutes",
                                 "1 hour",
                                 "2 hours"]
    
    private var sleepMode = 0
    private var sleepString = ""
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = whiteColor
        tab.rowHeight = 50
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        tab.isScrollEnabled = false
        return tab
    }()
    
    override func configView() {
        configNaviView(title: "Wi-Fi Settings")
        view.addSubview(tableView)
        getData()
    }
    
    private func getData() {
        
        net_getDeviceWifiTime { [weak self] (model) in
            if !isEmpty(string: model?.Sleep_interval) {
                self?.sleepString = ""
                if model!.Sleep_interval!.caseInsensitiveCompare("-1") == .orderedSame {
                    self?.sleepMode = 0
                }else if model!.Sleep_interval!.caseInsensitiveCompare("10") == .orderedSame {
                    self?.sleepMode = 1
                }else if model!.Sleep_interval!.caseInsensitiveCompare("30") == .orderedSame {
                    self?.sleepMode = 2
                }else if model!.Sleep_interval!.caseInsensitiveCompare("60") == .orderedSame {
                    self?.sleepMode = 3
                }else if model!.Sleep_interval!.caseInsensitiveCompare("120") == .orderedSame {
                    self?.sleepMode = 4
                }
                
            }else {
                self?.sleepString = "Wifi SSID"
            }
            self?.tableView.reloadData()
        }
        return;
        net_getDeviceInfoList {[weak self] (model) in
            if !isEmpty(string: model?.sysIdleTimeToSleep) {
                self?.sleepString = ""
                if model!.sysIdleTimeToSleep!.caseInsensitiveCompare("-1") == .orderedSame {
                    self?.sleepMode = 0
                }else if model!.sysIdleTimeToSleep!.caseInsensitiveCompare("10") == .orderedSame {
                    self?.sleepMode = 1
                }else if model!.sysIdleTimeToSleep!.caseInsensitiveCompare("30") == .orderedSame {
                    self?.sleepMode = 2
                }else if model!.sysIdleTimeToSleep!.caseInsensitiveCompare("60") == .orderedSame {
                    self?.sleepMode = 3
                }else if model!.sysIdleTimeToSleep!.caseInsensitiveCompare("120") == .orderedSame {
                    self?.sleepMode = 4
                }
                
            }else {
                self?.sleepString = "Wifi SSID"
            }
            self?.tableView.reloadData()
        }
    }
    
    private func setSleepMode(index:Int) {
        var time = "-1"
        switch sleepMode {
        case 1:
            time = "10"
        case 2:
            time = "30"
        case 3:
            time = "60"
        case 4:
            time = "120"
        default: break
        }
        net_setWifiReSetTime(time: time) {[weak self] (success) in
            self?.tableView.reloadData()
            if success {
//         self?.navigationController?.popViewController(animated: true)
            }
        }
    }
}

extension WiFiSettingsVC : UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = datas[indexPath.row].0
        cell.textLabel?.font = UIFont.systemFont(ofSize: 18)
        cell.textLabel?.textColor = textGray
        cell.selectionStyle = .none
        cell.accessoryType = .disclosureIndicator
        if indexPath.row == 2 {
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
            label.right = screenWidth - 30
            label.text = sleepString == "" ? sleepModeName[sleepMode] : sleepString
            label.font = cell.textLabel?.font
            label.textColor = cell.textLabel?.textColor
            label.backgroundColor = clearColor
            label.textAlignment = .right
            cell.contentView.addSubview(label)
        }
        let line = UIView(frame: CGRect(x: 8, y: 50 - 0.5, width: screenWidth - 16, height: 0.5))
        line.backgroundColor = textGray
        cell.contentView.addSubview(line)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard indexPath.row == 2 else {
            gotoNewVC(vcname: datas[indexPath.row].1)
            return
        }
        let alertView = PickerAlertView(title: "Select", data: sleepModeName)
        alertView.index = sleepMode
        alertView.handle = ({[weak self] index in
            self?.sleepMode = index
            self?.setSleepMode(index: index)
        })
        let alertVC = CustomAlertVC(contentType: .animation(alertView))
        alertVC.show()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
